<?php

use Controller\Mobile;
use think\Db;
use Elasticsearch\ClientBuilder;
/**
 * 首页
 * Class IndexController
 */
class EsController extends Mobile
{

    protected $needLoginActions = [];

    public function init()
    {

        parent::init();
        Yaf\Dispatcher::getInstance()->disableView();
    }


    /**
     * 首页
     */
    public function indexAction()
    {
        $client = ClientBuilder::create()->build();
        $params = [
            'index' => 'my_index',
            'id'    => 'my_id1',
            'body'  => ['testField' => 'abc']
        ];
        $params = [
            'index' => 'my_index',
            'id'    => 'my_id1',
            'body'  => ['testField' => 'abc']
        ];
        $response = $client->index($params);
        $params = [
            'index' => 'my_index',
            'body'  => [
                'query' => [
                    'match' => [
                        'testField' => 'abc'
                    ]
                ]
            ]
        ];

        $response = $client->search($params);
        print_r($response);

        echo 'es1';exit;
    }

    /**
     * @param array $user_bank
     */
    public function createAction()
    {
        $client = ClientBuilder::create()->setHosts(['127.0.0.1'])->build();
//        $params = [
//            'index' => 'myindexone', #index的名字不能是大写和下划线开头
//            'body' => [
//                'settings' => [
//                    'number_of_shards' => 2,
//                    'number_of_replicas' => 0
//                ]
//            ]
//        ];
//        $client->indices()->create($params);
        $params = [
            'index' => 'myindexone',
            'type' => 'mytype',
            'body' => [
                'mytype' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'id' => [
                            'type' => 'integer'
                        ],
                        'first_name' => [
                            'type' => 'text',
                            'analyzer' => 'ik_max_word'
                        ],
                        'last_name' => [
                            'type' => 'text',
                            'analyzer' => 'ik_max_word'
                        ],
                        'age' => [
                            'type' => 'integer'
                        ]
                    ]
                ]
            ]
        ];
        $client->indices()->putMapping($params);

        $params = [
            'index' => 'myindexone',
            'type' => 'mytype',
            'id' => 2, #可以手动指定id，也可以不指定随机生成
            'body' => [
                'first_name' => '王大虫',
                'last_name' => '五',
                'age' => 28
            ]
        ];
        $client->index($params);
        echo 'success!';
    }


    public function searchAction()
    {
        $client = ClientBuilder::create()->setHosts(['127.0.0.1'])->build();
//        $data = $client->search();
//        print_r($data);
//        $params = [
//            'index' => 'myindex',
//            'type' => 'mytype',
//            'id' =>'kYpvVnYBgjiCJD7oaB3J'
//        ];
//        $data = $client->get($params);
        $query = [
            'query' => [
                'bool' => [
                    'must' => [
                        'match' => [
                            'first_name' => '王名',
                        ]
                    ],
                    'filter' => [
                        'range' => [
                            'age' => [
                                'gt' => 1,
                                'lt' => 45
                            ]
                        ]
                    ]
                ]

            ]
        ];
        $params = [
            'index' => 'myindexone',
//  'index' => 'm*', #index 和 type 是可以模糊匹配的，甚至这两个参数都是可选的
//            'type' => 'mytype',
            '_source' => ['first_name','age'], // 请求指定的字段
            'body' => array_merge([
                'from' => 0,
                'size' => 5
            ],$query)
        ];
        $data = $client->search($params);
        print_r($data);
    }

}